package me.minecraft.woodenman.listener;

import me.minecraft.woodenman.WoodenMan;
import me.minecraft.woodenman.config.Language;
import me.minecraft.woodenman.runnable.GameTask;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerEvent implements Listener {

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();

        if (!WoodenMan.isPlayerInGame(player)) return;

        GameTask gameTask = WoodenMan.gameTask;
        if (!gameTask.isPlayerMove()) {
            event.setCancelled(true);
        }
        gameTask.handlePlayerMove(player);
    }

    @EventHandler
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
        Player player = event.getPlayer();
        Entity rightClicked = event.getRightClicked();

        if (!WoodenMan.isPlayerInGame(player)) return;

        WoodenMan.gameTask.handlePlayerInteraction(player, rightClicked);
    }

    //玩家离开游戏事件
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        // TODO 判断是否为游戏内的玩家 如果是则退出游戏 如果不是则不处理
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        // TODO 判断是否为游戏内的玩家 处理对退出游戏后加入的玩家
    }

    @EventHandler
    public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
        Player player = event.getPlayer();
        String command = event.getMessage().toLowerCase();

        if (player.isOp()) return;

        if (!WoodenMan.isPlayerInGame(player)) return;

        if ("/woodenmanquit".equals(command) || "/wmquit".equals(command)) {
            return;
        }

        player.sendMessage(Language.COMMAND_DISABLED_IN_GAME);
        event.setCancelled(true);
    }

}
